package cn.passlink.bletoothchat;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import cn.passlink.bletoothchat.BleGATT.BleGattServer;
import cn.passlink.bletoothchat.SqlTables.MyDatabaseHelper;
import cn.passlink.bletoothchat.Tools.Location.WifiLocationHelper;
import cn.passlink.bletoothchat.Tools.Permission.PermissionUtils;
import cn.passlink.bletoothchat.mActivity.RegisterActivity;
import cn.passlink.bletoothchat.mActivity.RelationshipActivity;
@SuppressLint("MissingPermission")
public class MainActivity extends AppCompatActivity {

    private SQLiteDatabase database;
    private MyDatabaseHelper databaseHelper;
    private BluetoothAdapter bluetoothAdapter;
    private BluetoothManager bluetoothManager;
    Context context;
    private  Intent RelationshipActivityintent;
    private  Intent RegisterActivityintent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;
        if (!PermissionUtils.checkPermissions(MainActivity.this)) {
            PermissionUtils.requestPermissions(MainActivity.this);
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {

                }
            }, 3000);
        }

        // 初始化蓝牙适配器
        bluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        bluetoothAdapter = bluetoothManager.getAdapter();

        WifiLocationHelper wifiLocationHelper = new WifiLocationHelper(context);
        // 检查蓝牙适配器是否为空和是否已启用
        if(!wifiLocationHelper.isWifiEnabled()){
            if (bluetoothAdapter == null || !bluetoothAdapter.isEnabled()) {
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivity(intent);
            }

        }



    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionUtils.onRequestPermissionsResult(MainActivity.this, requestCode, permissions, grantResults);
    }



    @Override
    protected void onResume() {
        super.onResume();
        databaseHelper = new MyDatabaseHelper(this,2);
        database=databaseHelper.getWritableDatabase();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                String[] columns = { "id" };
                String selection = "id = ?";
                String[] selectionArgs = { "1" };
                Cursor cursor = database.query("user", columns, selection, selectionArgs, null, null, null);
                if (cursor.moveToFirst()) {
                    // 在延迟后执行跳转操作
                    RelationshipActivityintent = new Intent(MainActivity.this, RelationshipActivity.class);
                    startActivity(RelationshipActivityintent);
                    finish();
                } else {
                    // 数据不存在
                    RegisterActivityintent = new Intent(MainActivity.this, RegisterActivity.class);
                    startActivity(RegisterActivityintent);
                    finish();
                }
                cursor.close();
            }
        }, 3000); // 延迟时间，单位为毫秒，这里设置为3秒

        //开启蓝牙服务
        startServer();
    }
    //开启服务
    private void startServer(){
        Intent serviceIntent;
        Toast.makeText(getApplication(), "BleGATT服务开启中.......", Toast.LENGTH_SHORT).show();
        // 执行查询操作
        Cursor cursor = database.rawQuery("SELECT username, phone FROM user WHERE id = 1", null);
        if (cursor != null && cursor.moveToFirst()) {
            int usernameIndex = cursor.getColumnIndex("username");
            int phoneIndex = cursor.getColumnIndex("phone");
            String username = cursor.getString(usernameIndex);
            String phone = cursor.getString(phoneIndex);
            //启动服务
            serviceIntent = new Intent(this, BleGattServer.class);
            serviceIntent.putExtra("name", username+"-"+phone);
            this.startService(serviceIntent);
        }
        else {
            Toast.makeText(getApplication(), "服务未启动........", Toast.LENGTH_SHORT).show();
        }
        // 关闭游标
        if (cursor != null) {
            cursor.close();
        }

    }
}